package cn.globalph.housekeeper.ui.dialog

import android.app.Dialog
import android.graphics.Bitmap
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import cn.globalph.housekeeper.R
import cn.globalph.housekeeper.utils.BitmapUtil
import cn.globalph.housekeeper.widgets.SignBoard

class UserSignDialog : BaseDialogFragment(){

    private lateinit var closeImage:ImageView
    private lateinit var resignTv:TextView
    private lateinit var ensureTv:TextView
    private lateinit var signBoard:SignBoard

    var listener:SignListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_user_sign,null)
        closeImage = view.findViewById(R.id.close_image)
        closeImage.setOnClickListener{
            dismiss()
        }

        signBoard = view.findViewById(R.id.sign_board)

        resignTv = view.findViewById(R.id.resign_btn)
        resignTv.setOnClickListener {
            signBoard.reSign()
        }

        ensureTv = view.findViewById(R.id.ensure_btn)
        ensureTv.setOnClickListener {
            saveSignBitmap()
        }

        var dialog = Dialog(context!!,R.style.Translucent_NoTitle)
        dialog.setContentView(view)
        val layoutParams = view.layoutParams
        layoutParams.width = resources.displayMetrics.widthPixels - 160
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
        view.layoutParams = layoutParams
        dialog.window!!.setGravity(Gravity.CENTER)
        return dialog
    }

    private fun saveSignBitmap(){
        var bmp = signBoard.getBitmap()
        if(bmp == null){
            TT("请签名")
            return
        }
        listener?.onSignClick(bmp)
        dismiss()
    }

    interface SignListener{
        fun onSignClick(bitmap:Bitmap)
    }
}